package com.sgolub.commanndsonline.jobs;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.sgolub.commanndsonline.domain.entity.AdvertisingRegistry;
import com.sgolub.commanndsonline.service.entity.AdvertisingRegistryService;
import com.sgolub.commanndsonline.service.entity.OperationItemService;
import com.sgolub.commanndsonline.ws.entity.AdvertisingRegistryItem;
import com.sgolub.commanndsonline.ws.entity.AdvertisingRegistryOperation;
import com.sgolub.commanndsonline.ws.service.AdvertisingRegistryItemService;
import com.sgolub.commanndsonline.ws.service.AdvertisingRegistryOperationService;

@Component("advertisingItemWorker")
public class AdvertisingItemWorkerImplAsync implements AdvertisingItemWorker {

	protected static Logger logger = Logger.getLogger("service");
	
	@Autowired
	private AdvertisingRegistryItemService srcService;
	
	@Autowired
	private AdvertisingRegistryService  advertisingRegistryService;	
	
	@Async
	 public void work() {
		  String threadName = Thread.currentThread().getName();
		  logger.debug("   " + threadName + " has began working.");
		         logger.debug("working...");
		         System.out.println("START * AdvertisingItemWorker *" + new Date().toLocaleString());
		         syncronizeOperations();
		         System.out.println("STOP * AdvertisingItemWorker *" + new Date().toLocaleString());		            
		   logger.debug("   " + threadName + " has completed work.");
		 }

	public void syncronizeOperations(){
		logger.debug("START * SYNC *" + new Date().toLocaleString());
		// Get unsynchronized Registry Items Items
		// If list is empty then return 
		List<AdvertisingRegistryItem> srcList  = srcService.listActive();
		
		logger.debug("START * SYNC SIZE = *" + srcList.size());
		if (srcList.size() == 0){
			System.out.println("END * SYNC * NOTHING TO SYNCHRONIZE");
			return;
		}
		
		// Iterate
		// Check
	    Iterator<AdvertisingRegistryItem> itrSrc = srcList.iterator();
	      while(itrSrc.hasNext()) {
	    	  AdvertisingRegistryItem elementSrc = itrSrc.next();

		    //Get Target Adverttizing Item Id
	    	 Integer currentTargetId =  
	    			 advertisingRegistryService.getItemByInventarNr(elementSrc.getInventarNr()).getId();

	    	 
	    	 AdvertisingRegistry newItem = copyFromWSItem(elementSrc);
	    	 if (currentTargetId != null) { //Item must be updated
	    		 newItem.setId(currentTargetId);
	    		 advertisingRegistryService.update(newItem);	    		
	    	 } else { // //Item must be inserted
	    		 advertisingRegistryService.add(newItem);
	    	 }
	    	 //logger.debug("elementSrc.getInventarNr() =" + elementSrc.getInventarNr() +" -> ITERATOR * currentTargetId="+currentTargetId);
	    	  
	    	 elementSrc.setOperationExecutedStatus(1);
	    	 srcService.update(elementSrc);
	      
	      }
		
	}
	
    public AdvertisingRegistry copyFromWSItem(AdvertisingRegistryItem elementSrc){
    	
		AdvertisingRegistry newItem = new AdvertisingRegistry();
		newItem.setExternalId(elementSrc.getExternalId());
		newItem.setInventarNr(elementSrc.getInventarNr());
		newItem.setSupportNr(elementSrc.getSupportNr());
		newItem.setSupportTypeId(elementSrc.getSupportTypeId());
		newItem.setSupportTypeName(elementSrc.getSupportTypeName());
		newItem.setRegionCod(elementSrc.getRegionCod());
		newItem.setRegionName(elementSrc.getRegionName());
		newItem.setLocalityCod(elementSrc.getLocalityCod());
		newItem.setLocalityName(elementSrc.getLocalityName());
		newItem.setAdress(elementSrc.getAdress());
		newItem.setLocationDescription(elementSrc.getLocationDescription());
		newItem.setStatus(elementSrc.getStatus());
		newItem.setPhoto(elementSrc.getPhoto());
		newItem.setPrice_base(elementSrc.getPrice_base());
		newItem.setPrice_vip_a(elementSrc.getPrice_base());
		newItem.setPrice_vip_b(elementSrc.getPrice_base());
		newItem.setWidth(elementSrc.getWidth());
		newItem.setHeight(elementSrc.getHeight());
		newItem.setLight(elementSrc.getLight());
		
		return newItem;
    }	

}
